<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 完全匹配
      const r1 =
        /^(?<Y>\d{4})-(?<M>\d\d)-(?<D>\d\d) (?<h>\d\d):(?<m>\d\d):(?<s>\d\d)$/;
      const str = "2024-01-04 11:30:40";
      console.log(str.match(r1)); //推荐

      const r = str.match(r1);
      console.log(r.groups.Y);
      console.log(r.groups.M);
      console.log(r.groups.D);
      console.log(r.groups.h);
      console.log(r.groups.m);
      console.log(r.groups.s);
    </script>
  </body>
</html>
